package jia.temp;

import jia.game.GenericFlags;
import jia.core.GameStateManager;
import jia.core.calculations.Area;
import jia.core.calculations.Dice;
import jia.core.characters.GameCharacter;
import jia.core.items.Weapon;
import jia.core.items.Item;
import jia.core.media.Sprite;
import jia.core.state.CombatState;
import org.apache.log4j.Logger;

import java.awt.*;
import java.awt.image.BufferedImage;
import java.util.ArrayList;

public class PrimitiveCombatData{
    private static Area areaCalc = new Area();
    private GameStateManager mgr;
    private static final Logger log = Logger.getLogger(PrimitiveCombatData.class);
    
    
    public PrimitiveCombatData(GameStateManager thisMgr){
        mgr = thisMgr;
        
    }
    
    
    /**
     * @return
     */
    public ArrayList<CombatState> setupCombatants(){
        ArrayList<CombatState> fullList = new ArrayList<CombatState>();
        int outsideCounter = 12;
        
        //get the list of heros
        GameCharacter[] characters = mgr.getGameCharacters();
        
        
        //iterate through the hro list, setting up a combat state for each.
        for(int i = 0; i < characters.length; i++) {
            GameCharacter character = characters[i];
            
            CombatState heroState = new CombatState(true, 0, character, false, outsideCounter);
            heroState.setInitiative(character.getBaseInitiative() + Dice.roll(20, 1));
            //set this hero's XY point
            if (i < 3){
                heroState.setXY(new Point((i+1)+(characters.length +1), 10));
            }else{
                heroState.setXY(new Point((i+1)+((characters.length +1)-2), 11));
            }
            
            //load the character's sprite
            BufferedImage mainSprite = null;
            BufferedImage spriteDefault = null;
            BufferedImage spriteAttack = null;
            try {
                mainSprite = Sprite.getMainSpriteSheet(character.getSpritePath());
                spriteDefault = Sprite.getDefaultSprite(mainSprite);
                spriteAttack = Sprite.getAttackSprite(mainSprite);
                
                heroState.setSprite(Sprite.returnScaledImage(spriteDefault));
                heroState.setAttackSprite(Sprite.returnScaledImage(spriteAttack));
            } catch(Exception e) {
                if (GenericFlags.errorMessages){
                    log.error(e.getMessage());
                }
            }
            
            //get the surrounding squares
            heroState.setSurroundingSquares(areaCalc.getSurroundingSquares(heroState.getXY()));
            
            //finally, make sure the hero isnt dead
            if (character.getCharacterHP() <= 0){
                character.setDead(true);
            }
            
            //add this hero to the list
            fullList.add(heroState);
            outsideCounter--;
            
        }
        int insideCounter = characters.length+1;
        //now loop through the enemies list as defined in the encounter event
        for (int j = 0; j < 6; j++){
            //create the enemies
            GameCharacter drac = new Draconian().init(insideCounter);
            CombatState enemyState = new CombatState(false, 0, drac, true, outsideCounter);
            enemyState.setInitiative(drac.getBaseInitiative() + Dice.roll(20, 1));
            //place the enemies on the screen
            if (j < 3){
                enemyState.setXY(new Point(((6+2)-(j+1)), 4));
            }else{
                enemyState.setXY(new Point(((6+2)-(j+1)), 3));
            }
            
            //load the sprites
            //load the character's sprite
            BufferedImage mainSprite = null;
            BufferedImage spriteDefault = null;
            BufferedImage spriteAttack = null;
            try {
                mainSprite = Sprite.getMainSpriteSheet(drac.getSpritePath());
                spriteDefault = Sprite.getDefaultSprite(mainSprite);
                spriteAttack = Sprite.getAttackSprite(mainSprite);
                
                enemyState.setSprite(Sprite.returnScaledImage(spriteDefault));
                enemyState.setAttackSprite(Sprite.returnScaledImage(spriteAttack));
            } catch(Exception e) {
                if (GenericFlags.errorMessages){
                    log.error(e.getMessage());
                }
            }
            
            //get the surrounding squares
            enemyState.setSurroundingSquares(areaCalc.getSurroundingSquares(enemyState.getXY()));
            
            //add this enemy to the list
            fullList.add(enemyState);
            outsideCounter--;
            insideCounter++;
        }
        
        return fullList;
    }
    
    /**
     * @param characterState
     */
    public static void sendCharacterInformationToLog(CombatState characterState, Item weapon){
        Weapon adjWepn = (Weapon) weapon;
        if(GenericFlags.debugMessages) {
            log.debug("*************Character Sheet for " + characterState.getCharacter().getCharacterName() + "*************");
            log.debug("STR: " + characterState.getCharacter().getStrength() + " (" + characterState.getCharacter().getStrengthMod() + ")");
            log.debug("DEX: " + characterState.getCharacter().getDexterity() + " (" + characterState.getCharacter().getDexterityMod() + ")");
            log.debug("CON: " + characterState.getCharacter().getConstitution() + " (" + characterState.getCharacter().getConstitutionMod() + ")");
            log.debug("INT: " + characterState.getCharacter().getIntelligence() + " (" + characterState.getCharacter().getIntelligenceMod() + ")");
            log.debug("WIS: " + characterState.getCharacter().getWisdom() + " (" + characterState.getCharacter().getWisdomMod() + ")");
            log.debug("CHA: " + characterState.getCharacter().getCharisma() + " (" + characterState.getCharacter().getCharismaMod() + ")");
            log.debug("");
            log.debug("Initiative is " + characterState.getInitiative() + " (" + characterState.getCharacter().getDexterityMod() + " points of which are based upon a Dexterity Modifier)");
            log.debug("");
            log.debug("HP: " + characterState.getCharacter().getCharacterHP() + " (" + characterState.getCharacter().getConstitutionMod() + " points of which is based on a Constitution Modifier)");
            log.debug("");
            log.debug("");
            log.debug("Readied Weapon: " + characterState.getCharacter().getReadiedWeapon().getName() + " with a damage value of " + adjWepn.getDamageDie() + "D" + adjWepn.getDamageDieSides());
            log.debug("");
            log.debug("");
        }
    }
    
    /**
     * Sets the current characters hit points based upon a die roll and their constitution modifier
     *
     * @param characterState
     */
    public static void setHP(CombatState characterState){
        try {
            int roll = 10;
            int hp = roll + characterState.getCharacter().getConstitutionMod();
            characterState.getCharacter().setCharacterHP(hp);
            
            if (GenericFlags.debugMessages) {
                log.debug(characterState.getCharacter().getCharacterName() + "'s hit points have been set to " + roll + " plus modifier value of " + characterState.getCharacter().getConstitutionMod() + ", equalling " + characterState.getCharacter().getCharacterHP());
            }
        } catch(Exception e) {
            if (GenericFlags.errorMessages){
                log.error(e.getMessage());
            }
        }
    }
}